package com.book.controller.admin;

import com.book.pojo.User;
import com.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    // 获取用户列表
    @GetMapping("/list")
    public String list(Model model){

        List<User> userList = userService.list();

        model.addAttribute("userList", userList);

        return "admin/users_list";
    }


    // 跳转到修改页面
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){

        User user = userService.getById(id);

        model.addAttribute("users", user);

        return "admin/users_update";
    }


    // 修改
    @PostMapping("/update")
    public String update(User user){
        userService.updateById(user);
        return "redirect:/admin/user/list";
    }
}
